package com.geek.concurrence4work;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author: lishuangqiang
 * @Date: 2021/11/28
 * @Description:
 */

public class CyclicBarrierMethod {

    private static class MyThread extends Thread {
        private static volatile long res = 0;

        private CyclicBarrier cyclicBarrier;

        private MyThread(CyclicBarrier cyclicBarrier) {
            this.cyclicBarrier = cyclicBarrier;
        }

        @Override
        public void run() {
            res = Homework03.sum3();
            try {
                cyclicBarrier.await();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        private static long getRes() {
            return res;
        }
    }

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(1, new Runnable() {
            @Override
            public void run() {
                //这是得到的返回值
                long result = MyThread.getRes();
                // 确保  拿到result 并输出
                System.out.println("异步计算结果为：" + result);

            }
        });
        MyThread mythread = new MyThread(cyclicBarrier);
        mythread.start();

    }


}
